package com.situ.web.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.Course;
import com.situ.web.service.impl.CourseServiceImpl;
import com.situ.web.util.JDBCUtil;
import com.situ.web.vo.PageInfo;

@WebServlet("/course")
public class CourseServlet extends HttpServlet{
	private CourseServiceImpl courseService = new CourseServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String method = req.getParameter("method");
		if(method == null || "".equals(method)) {
			method = "selectPage";
		}
		switch(method) {
		case "selectAll":
			selectAll(req, resp);
			break;
		case "selectName":
			selectName(req, resp);
			break;
		case "delete":
			delete(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "selectId":
			selectId(req, resp);
			break;
		case "selectPage":
			selectPage(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		default:
			break;
		}
	}
	
		private void selectPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		
		if (pageNo == null || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = "5";
		}
		
		PageInfo pageInfo = courseService.selectPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		
		req.setAttribute("pageInfo", pageInfo);
		req.getRequestDispatcher("course_page_list.jsp").forward(req, resp);
	}
		
		private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
			String id = req.getParameter("id");
			String name = req.getParameter("name");
			String credit = req.getParameter("credit");
			Course course = new Course(Integer.parseInt(id), name, Integer.parseInt(credit));
			boolean isSuccess = courseService.update(course);
			System.out.println(isSuccess);
			

			resp.sendRedirect(req.getContextPath() + "/course?method=selectPage");
		
	}
		
		private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
			// 1、得到前台传递过来的数据
			String id = req.getParameter("id");
			String name = req.getParameter("name");
			String credit = req.getParameter("credit");
			if(id == "") {
				resp.sendRedirect(req.getContextPath() + "/course?method=selectAll");
				return;
			}
			if(credit == "") {
				credit = "0";
			}
			// 封装成Course对象
			Course course = new Course(Integer.parseInt(id), name, Integer.parseInt(credit)); 
			// 2、调用service代码完成功能
			boolean isSuccess = courseService.insert(course);
			System.out.println(isSuccess);
			// 3、页面跳转
			// req.getContextPath()得到该项目的名字
			resp.sendRedirect(req.getContextPath() + "/course?method=selectPage");
		
	}
		
		private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
			String deleteId = req.getParameter("id");
			String pageNo = req.getParameter("pageNo");
			int id = Integer.parseInt(deleteId);
			boolean isSuccess = courseService.delete(id);
			System.out.println(isSuccess);
			
			resp.sendRedirect(req.getContextPath() + "/course?method=selectPage&pageNo=" + pageNo);
		
	}
		
		private void selectName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			String selectName = req.getParameter("selectName");
			
			Connection connection = null;
			PreparedStatement statement = null;
			ResultSet resultSet = null;
			ArrayList<Course> list = new ArrayList<Course>();
			try {
				connection = JDBCUtil.getConnection();
				String sql = "SELECT `id`,`name`,`credit` FROM `course` where name like ?";
				statement = connection.prepareStatement(sql);
				statement.setString(1, "%" + selectName + "%");
				System.out.println(statement);
				resultSet = statement.executeQuery();
				while (resultSet.next()) {
					int id = resultSet.getInt("id");
					String name = resultSet.getString("name");
					int credit = resultSet.getInt("credit");
					Course course = new Course(id, name, credit);
					list.add(course);
				}
				for (Course course : list) {
					System.out.println(course);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				JDBCUtil.close(connection, statement, resultSet);
			}
			
			// 把数据放到request里面，就相当于一块内存区域
			req.setAttribute("list", list);
			// 转发（跳转）到student_list.jsp页面进行展示
			req.getRequestDispatcher("course_list.jsp").forward(req, resp);
		
	}
		
		private void selectId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			String selectId = req.getParameter("id");
			
			Course course = courseService.selectId(Integer.parseInt(selectId));
			
			req.setAttribute("course", course);
			req.getRequestDispatcher("course_update.jsp").forward(req, resp);
			
		}
		
		private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			List<Course> list = courseService.selectAll(); 
			
			// 把list集合数据放到request里面，就相当于一块内存区域
			req.setAttribute("list", list);
			// 转发（跳转）到student_list.jsp页面进行展示
			req.getRequestDispatcher("course_list.jsp").forward(req, resp);
	}
}
